package cc;

//Component
interface Car {
	// 多少钱
	int howMuch();

	// 展示装配的配件
	void showParts();
}

//ConcreteComponents
class Suv implements Car {
	public Suv(String owner) {
		System.out.println(owner + "买了一辆Suv，10W");
	}

	@Override
	public int howMuch() {
		return 10;
	}

	@Override
	public void showParts() {
		// 空实现
	}
}

class Mpv implements Car {
	public Mpv(String owner) {
		System.out.println(owner + "买了一辆Mpv，15W");
	}

	@Override
	public int howMuch() {
		return 15;
	}

	@Override
	public void showParts() {
		// 空实现
	}
}

//Decorator
class CarParts implements Car {
	private Car car;

	@Override
	public int howMuch() {
		return car != null ? car.howMuch() : 0;
	}

	@Override
	public void showParts() {
		if (car != null) {
			car.showParts();
		}
	}

	public Car decorator(Car car) {
		this.car = car;
		return this;
	}
}

//ConcreteDecorators
class Sofa extends CarParts {
	@Override
	public int howMuch() {
		return super.howMuch() + 1;
	}

	@Override
	public void showParts() {
		super.showParts();
		System.out.println("选配了真皮沙发，1W");
	}
}

class Safety extends CarParts {
	@Override
	public int howMuch() {
		return super.howMuch() + 3;
	}

	@Override
	public void showParts() {
		super.showParts();
		System.out.println("选配了全套安全系统，3W");
	}
}

class Engine extends CarParts {
	@Override
	public int howMuch() {
		return super.howMuch() + 5;
	}

	@Override
	public void showParts() {
		super.showParts();
		System.out.println("选配了V8发动机，5W");
	}
}

//测试代码
public class zs {
	public static void main(String[] args) {
		// cc买一辆suv
		Car suv = new Suv("cc");
		// 装配沙发
		suv = new Sofa().decorator(suv);
		// 装配发动机
		suv = new Engine().decorator(suv);
		// 装配安全系统
		suv = new Safety().decorator(suv);
		suv.showParts();
		System.out.println("一共花了" + suv.howMuch() + "W");

		// xx买一辆mpv
		Car mpv = new Mpv("xx");
		// 装配发动机
		mpv = new Engine().decorator(mpv);
		// 装配安全系统
		mpv = new Safety().decorator(mpv);
		mpv.showParts();
		System.out.println("一共花了" + mpv.howMuch() + "W");
	}
}
